package main.menu;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;

import main.staff.CreateTypesSql;
import main.util.PYUtil;
import main.util.ReplaceSQLString;
import main.util.SqlConstants;

public class ReplaceString {
	
	public static ReplaceSQLString rss=new ReplaceSQLString();
	
	public static void main1(String[] args) {
		File file=new File("E:/客服融合菜单脚本");
		File[] files=file.listFiles();
		
		
		for(int i=0;i<files.length;i++){
			if(files[i].isDirectory()){
				continue;
			}
			
			String fileName=files[i].getName();
			if(!fileName.contains(".sql")){
				System.out.println(fileName+"不能复制");
				continue;
			}
			
			
			File dir=new File("E:/客服融合菜单脚本_修改/");
			if(!dir.exists()){
				dir.mkdirs();
			}
			File copeFile=new File(dir, fileName);
			
			//replaceFile(files[i], copeFile);
			
		
		}		
	}
	
	/*根据sql语句生成用户密码信息*/
	public static void main10(String[] args){
		File origin=new File("E:/t_ucp_staffbasicinfo.sql");
		File out=new File("E:/t_ucp_password.sql");
		
		
		try {
			FileWriter fw = new FileWriter(out);
			FileInputStream fis = new FileInputStream(origin);
			BufferedReader bh = new BufferedReader(new InputStreamReader(fis));
			String sql = bh.readLine();
			//SqlConstants.topWriteHead(fw);
			while (sql != null) {
				String staffID=rss.getColNameValue(sql,"STAFFID");
				if(staffID.equals("")){
					fw.write(sql);
					fw.write("\n");
				}else
					fw.write(CreateTypesSql.password(staffID));
				//
				sql = bh.readLine();
			}
			fw.close();
			bh.close();
		} catch (Exception e) {
		}
	}
	
	//替换sql里面的字符串
	public static void main(String[] args){
		File origin=new File("E:/权限角色相关工作流.sql");
		File out=new File("E:/t_ucp_staffbasicinfo——.sql");
		replaceFileAddCols(origin,out);
	}
	
	
	/*替换打开方式OPENMODULE、和添加缩写字母ABBREVIATION*/
	public static void replaceFileAddCols(File origin,File resultFile){
		try {
			FileWriter fw = new FileWriter(resultFile);
			FileInputStream fis = new FileInputStream(origin);
			BufferedReader bh = new BufferedReader(new InputStreamReader(fis));
			String readLine = bh.readLine();
			while (readLine != null) {
				String sql = rss.getSQLReplaceCol(readLine,"OPENMODULE","N");
				String menuName=rss.getColNameValue(sql,"MENUNAME");
				if(menuName!=null && !"".equals(menuName)){
					sql=rss.getSQLAddCol(sql, "ABBREVIATION", PYUtil.getAllFirstLetter(menuName));
				}
				fw.write(sql);
				fw.write("\n");
				//System.out.println(sql);
				readLine = bh.readLine();
				System.out.println(readLine);
			}
			fw.close();
			bh.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/*替换链接*/
	public static void replaceFileURL(File origin,File resultFile){
		try {
			FileWriter fw = new FileWriter(resultFile);
			FileInputStream fis = new FileInputStream(origin);
			BufferedReader bh = new BufferedReader(new InputStreamReader(fis));
			String readLine = bh.readLine();
			while (readLine != null) {
				String sql = rss.getReplaceURLStr(readLine);
				fw.write(sql);
				fw.write("\n");
				//System.out.println(sql);
				readLine = bh.readLine();
			}
			fw.close();
			bh.close();
		} catch (Exception e) {
		}
	}

}
